package io.adrainty.algorithm.leetcode.lcr;

/**
 * LCR 012. 寻找数组的中心下标
 * @link <a href="https://leetcode.cn/problems/tvdfij/">...</a>
 * @author AdRainty
 * @version V1.0.0
 * @since 2024/9/8 下午10:06
 */
public class LCR012 {

    public int pivotIndex(int[] nums) {
        // 数组的和
        int sum = 0;
        for (int num : nums) {
            sum += num;
        }
        int leftSum = 0;
        for (int i = 0; i < nums.length; i++) {
            leftSum += nums[i];
            // 如果相等，返回当前下标
            if (leftSum - nums[i] == sum - leftSum) {
                return i;
            }

        }
        return -1;
    }

}
